package map;

import java.awt.Button;
import java.awt.Color;
import java.awt.Label;
import java.awt.Panel;
import java.awt.TextField;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JPanel;

import processing.core.PApplet;

public class Request {
	
	private static Panel panel;
	private static TextField field;
	private static Button button, b1, b2;
	private static Label label;
	private static Map map;
	
	public static void init(final PApplet applet, final Map map){
		Request.map=map;
		panel = new Panel();
		panel.setBounds(0, 0, 250, 50);
		panel.setVisible(false);
		panel.setBackground(Color.WHITE);
		panel.setFocusable(false);
		
		label=new Label("search for:");
		label.setBounds(0, 0, 70, 25);
		label.setVisible(true);
		
		field = new TextField();
		field.setBounds(label.getWidth(), 0, 100, 25);
		field.addKeyListener(new KeyListener() {
			@Override
			public void keyPressed(KeyEvent arg0) {
				if (10==arg0.getKeyCode()){
					Request.changeVisible();
					applet.requestFocus();
					Request.search(field.getText());
				}
			}
			@Override
			public void keyReleased(KeyEvent e) {}
			@Override
			public void keyTyped(KeyEvent e) {}
		});
		
		button=new Button("search");
		button.setVisible(true);
		button.setBounds(label.getWidth()+field.getWidth()+5, 0, 75, 25);
		button.setFocusable(false);
		button.addMouseListener(new MouseListener() {
			@Override
			public void mouseReleased(MouseEvent arg0) {}
			@Override
			public void mousePressed(MouseEvent arg0) {}
			@Override
			public void mouseExited(MouseEvent arg0) {}
			@Override
			public void mouseEntered(MouseEvent arg0) {}
			@Override
			public void mouseClicked(MouseEvent arg0) {Request.search(field.getText()); Request.changeVisible(); applet.requestFocus();}
		});
		
		b1=new Button("reset Station");
		b1.setBounds(0, 25, 120, 25);
		b1.setVisible(true);
		b1.setFocusable(false);
		b1.addMouseListener(new MouseListener() {
			@Override
			public void mouseReleased(MouseEvent e) {}
			@Override
			public void mousePressed(MouseEvent e) {}
			@Override
			public void mouseExited(MouseEvent e) {}	
			@Override
			public void mouseEntered(MouseEvent e) {}
			
			@Override
			public void mouseClicked(MouseEvent e) {
				map.restore();
				Request.changeVisible();
				applet.requestFocus();
			}
		});
		
		b2=new Button("change View");
		b2.setBounds(125, 25, 120, 25);
		b2.setVisible(true);
		b2.setFocusable(false);
		b2.addMouseListener(new MouseListener() {
			@Override
			public void mouseReleased(MouseEvent e) {}
			@Override
			public void mousePressed(MouseEvent e) {}
			@Override
			public void mouseExited(MouseEvent e) {}	
			@Override
			public void mouseEntered(MouseEvent e) {}
			
			@Override
			public void mouseClicked(MouseEvent e) {
				map.changeView();
				Request.changeVisible();
				applet.requestFocus();
			}
		});
		
		panel.add(b1);
		panel.add(b2);
		panel.add(button);
		panel.add(label);
		panel.add(field);
		applet.add(panel);
	}
	
	public static void changeVisible(){
		panel.setVisible(!panel.isVisible());
		if (panel.isVisible()) {field.requestFocus(); field.selectAll();}
	}
	
	public static void search(String s){
		map.searchForMainStation(s);
	};

}
